#include "Kit.h"
#include "Part.h"


Kit::Kit(int startX, int startY, QGraphicsScene *scene)
	: GUIBase()
{
	loadImage("Resources/Sprites/Kit.png");

	this->setX(startX);
	this->setY(startY);
	this->setZValue(ZVALUE_KIT);


	parts = new std::vector<Part*>(8);
	for(int ii=0 ; ii<8 ; ii++)
	{
		(*parts)[ii] = NULL;
	}
	number_parts = 0;
}

Kit::Kit()
	: GUIBase()
{
	parts = new std::vector<Part*>();
	number_parts = 0;
}

Kit::~Kit()
{
	delete parts;
}

void Kit::doneScaling()
{
	this->setScaleSpeed(0.1);
}

void Kit::addPart(Part *p)
{
	for(int ii=0 ; ii<8 ; ii++)
	{
		if(this->partTypesNeeded.size()==ii)
			break;
		if(p->partType==this->partTypesNeeded[ii] && (*parts)[ii]==NULL)
		{
			(*parts)[ii]=p;
			p->setFlag(QGraphicsItem::ItemStacksBehindParent, false);
			p->setParentItem(this);
			//p->setX(7+9*number_parts);	//original
			p->setX(12+12*ii);		//Keith edited to use the correct png size: 12x12
			p->setY(14);
			number_parts++;
			p=NULL;
			break;
		}
	}
	if(p!=NULL)
	{
		for(int ii=0 ; ii<8 ; ii++)
		{
			if((*parts)[ii]==NULL)
			{
				(*parts)[ii]=p;
				p->setFlag(QGraphicsItem::ItemStacksBehindParent, false);
				p->setParentItem(this);
				//p->setX(7+9*number_parts);	//original
				p->setX(7+12*ii);		//Keith edited to use the correct png size: 12x12
				p->setY(14);
				number_parts++;
				break;
			}
		}
	}
	//parts->push_back(p);
	//p->setX(this->x()+10*number_parts);
	
	
}

void Kit::addPart(Part *p, int ind)
{
	if(parts->at(ind) == NULL)
		number_parts++;
	//Sean says: you must make yourself the parent of the part I add to you
	parts->at(ind) = p;
	p->setFlag(QGraphicsItem::ItemStacksBehindParent);
	p->setParentItem(this);
	//p->setX(this->x()+5*number_parts);	//original
	p->setX(12*number_parts);		//Keith added to make it fit the parts' size: 12x12

	p->setY(5);
	
	number_parts++;
}

Part* Kit::getPart(int ind)
{
	return parts->at(ind);
}

bool Kit::isComplete()
{
	bool complete = true;
	bool partExists = false;
	std::vector<bool> partsUsed(parts->size()); //make sure a single part isn't counted twice

	//Alex Silkin says: you did not initialize the bools!!!
	for ( int i = 0; i < partsUsed.size(); i++ )
		partsUsed.at(i) = false;


	if ( parts->size() < partTypesNeeded.size() )
	{
		complete = false;
	}
	else 
	{
		for ( int i = 0; i < partTypesNeeded.size(); i++ ) // Alex says: why ++i, shouldn't it be i++ ?
		{
			partExists = false;
			for ( int j = 0; j < parts->size(); j++)
			{
				if ( parts->at( j ) != NULL )
				{
					if ( parts->at( j )->partType == partTypesNeeded.at( i ) && !partsUsed.at( j ) )
					{
						partExists = true;
						partsUsed.at( j ) = true;
						break;
					}
				}
				else 
				{
					complete = false;
					break;
				}
			}
			if ( !partExists )
			{
				complete = false;
				break;
			}
		}
	}

	return complete;
}

void Kit::removePart(int ind)
{
	if(parts->at(ind) != NULL)
	{
		parts->at(ind)->setParentItem(NULL);
		parts->at(ind) = NULL;
		number_parts--;
		
	}
}

std::vector<PartType> Kit::getPartTypesNeeded()
{
	return this->partTypesNeeded;
}

bool Kit::isFull()
{
	return ( number_parts == partTypesNeeded.size() );
	//return (number_parts == 8 );
}